/**
 * Created With IntelliJ IDEA
 * Description:牛客网：BM80 买卖股票的最好时机(一)
 * https://www.nowcoder.com/practice/64b4262d4e6d4f6181cd45446a5821ec?tpId=295&tqId=625&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj
 * User: DELL
 * Data: 2022-11-14
 * Time: 22:52
 */

import java.util.*;

public class Solution {
    public int maxProfit (int[] prices) {
        //当prices的长度小于2时，将无法做到买入必须在卖出的前面的某一天，因此利润为0
        if (prices.length < 2) {
            return 0;
        }
        int max = 0;   //遍历时，记录已遍历部分的最大利润
        int min = prices[0];   //遍历时，记录已遍历元素的最小值
        for (int i = 1; i < prices.length; i++) {
            max = Math.max(max,prices[i] - min);
            min = Math.min(min,prices[i]);
        }
        return max;
    }
}